package com.molichuxing.service.impl;

import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.service.ThirdMotJobService;
import com.molichuxing.services.business.dto.request.modify.YearlyInspectionModifyBizDto;
import com.molichuxing.services.business.dto.response.ThirdMotStateDto;
import com.molichuxing.services.business.service.CarYearlyInspectionBizService;
import com.molichuxing.services.business.service.MotBizService;
import com.molichuxing.services.infrastructure.service.YearlyInspectService;
import com.molichuxing.services.property.YearlyInspectHandleStatusEnum;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: liws
 * @Description:
 * @Date: Created in 17:04 2019/11/14
 * @Modified By:
 **/
@Service("thirdMotJobService")
public class ThirdMotJobServiceImpl implements ThirdMotJobService {

    private static final Logger log = Logger.getLogger(ThirdMotJobServiceImpl.class);

    @Resource
    MotBizService motBizService;

    @Resource
    YearlyInspectService yearlyInspectService;

    @Resource
    CarYearlyInspectionBizService carYearlyInspectionBizService;

    @Override
    public Boolean queryThirdMotState() throws Exception {

        try {
            List<Integer> list1 = yearlyInspectService.getCarIdListByHandleStatus(YearlyInspectHandleStatusEnum.IN_HANDLE);
            List<Integer> list0 = yearlyInspectService.getCarIdListByHandleStatus(YearlyInspectHandleStatusEnum.NO_HANDLE);

            List<Integer> carIdList = new ArrayList<>();
            if (list1 != null && list1.size() > 0) {
                carIdList.addAll(list1);
            }

            if (list0 != null && list0.size() > 0) {
                carIdList.removeAll(list0);
                carIdList.addAll(list0);
            }

            log.info("第三方年检 >>> 待查车辆数量："+carIdList.size());

            if (carIdList != null && carIdList.size() > 0) {
                ResponseResult<List<ThirdMotStateDto>> res = motBizService.getThirdMotState(carIdList);
                log.info("第三方年检 >>> 外部接口返回结果："+(res==null?"null":res.toString()));
                if (res != null && res.getCode() != null && res.getCode() == 0) {
                    List<ThirdMotStateDto> resultList = res.getData();
                    if (resultList != null && resultList.size() > 0) {
                        List<YearlyInspectionModifyBizDto> paramList = new ArrayList<>();
                        for (ThirdMotStateDto stateDto : resultList) {
                            if(stateDto!=null){
                                YearlyInspectionModifyBizDto bizDto = new YearlyInspectionModifyBizDto();
                                bizDto.setCarId(stateDto.getCarId());
                                bizDto.setExpressNumber(stateDto.getEmsComeno());
                                bizDto.setHandleStatus(stateDto.getOrderState());
                                bizDto.setQualificationCertificateUrl(stateDto.getInsureProveUrl());
                                bizDto.setReturnReason(stateDto.getCancelReason());
                                paramList.add(bizDto);
                            }
                        }
                        if(paramList.size()>0){
                            log.info("第三方年检 >>> 车辆服务接口参数："+paramList);
                            try {
                                carYearlyInspectionBizService.modifyYearlyInspection(paramList);
                            }catch (Exception e){
                                e.printStackTrace();
                                log.error("第三方年检 >>> 车辆服务接异常："+e.getMessage());
                            }

                        }
                    }
                }
            }else{
                log.info("第三方年检 >>> 待查车辆小于1");
            }

        } catch (Exception e) {
            log.error("查询第三方年检车辆状态异常：" , e);
            throw new Exception("查询第三方年检车辆状态异常");
        }
        return true;



    }
}
